﻿package mortal.game.resource.tableConfig
{
    import Message.DB.Tables.*;
    import __AS3__.vec.*;
    import com.gengine.resource.*;
    import flash.utils.*;
    import mortal.common.tools.*;
    import mortal.game.cache.*;
    import mortal.game.control.*;
    import mortal.game.manager.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.game.view.skill.*;

    public class ShopConfig extends Object
    {
        private var _shopMap:Dictionary;
        private var _rebateItemMap:Dictionary;
        private var _rebateShopMap:Dictionary;
        private var _shopTypeItemVec:Dictionary;
        private var _shopTypeItemMap:Dictionary;
        public static const fastBuyShopGroup:int = 3009;
        private static var _instance:ShopConfig;

        public function ShopConfig()
        {
            this._shopMap = new Dictionary();
            this._rebateItemMap = new Dictionary();
            this._rebateShopMap = new Dictionary();
            this._shopTypeItemVec = new Dictionary();
            this._shopTypeItemMap = new Dictionary();
            if (_instance != null)
            {
                throw new Error(" ShopConfig 单例 ");
            }
            this.init();
            return;
        }// end function

        private function write(param1:Object) : void
        {
            var _loc_2:TShopSell = null;
            var _loc_3:Object = null;
            for each (_loc_3 in param1)
            {
                
                _loc_2 = new TShopSell();
                this.changeToDate(_loc_3);
                ClassTypesUtil.copyValue(_loc_2, _loc_3);
                if (this._shopTypeItemVec[_loc_2.shopCode] == null)
                {
                    this._shopTypeItemVec[_loc_2.shopCode] = new Vector.<TShopSell>;
                }
                (this._shopTypeItemVec[_loc_2.shopCode] as Vector.<TShopSell>).push(_loc_2);
                if (this._shopTypeItemMap[_loc_2.shopCode] == null)
                {
                    this._shopTypeItemMap[_loc_2.shopCode] = new Dictionary();
                }
                this._shopTypeItemMap[_loc_2.shopCode][_loc_2.itemCode] = _loc_2;
            }
            return;
        }// end function

        private function writePanic(param1:Object) : void
        {
            var _loc_2:TShopPanicBuyItem = null;
            var _loc_3:Object = null;
            for each (_loc_3 in param1)
            {
                
                _loc_2 = new TShopPanicBuyItem();
                ClassTypesUtil.copyValue(_loc_2, _loc_3);
                this._rebateItemMap[_loc_2.code + "_" + _loc_2.itemCode] = _loc_2;
                if (this._rebateShopMap[_loc_2.code] == null)
                {
                    this._rebateShopMap[_loc_2.code] = new Array();
                }
                (this._rebateShopMap[_loc_2.code] as Array)[(_loc_2.index - 1)] = _loc_2;
            }
            return;
        }// end function

        private function writeShop(param1:Object) : void
        {
            var _loc_2:TShop = null;
            var _loc_3:Object = null;
            for each (_loc_3 in param1)
            {
                
                _loc_2 = new TShop();
                ClassTypesUtil.copyValue(_loc_2, _loc_3);
                this._shopMap[_loc_2.code] = _loc_2;
            }
            return;
        }// end function

        private function getArenaArr(param1:Vector.<TShopSell>) : Vector.<TShopSell>
        {
            var _loc_5:ItemData = null;
            var _loc_7:TShopSell = null;
            var _loc_2:* = new Vector.<TShopSell>;
            var _loc_3:* = Cache.instance.role.entityInfo.level;
            var _loc_4:* = ClockManager.instance.nowDate.time;
            var _loc_6:int = 0;
            for each (_loc_7 in param1)
            {
                
                if (_loc_7.extendStr == _loc_6.toString())
                {
                    if (this.isCanSellByTshopSell(_loc_7, _loc_3, _loc_4))
                    {
                        _loc_2.push(_loc_7);
                    }
                }
            }
            return _loc_2;
        }// end function

        private function get2v2ArenaArr(param1:Vector.<TShopSell>) : Vector.<TShopSell>
        {
            var _loc_5:ItemData = null;
            var _loc_7:TShopSell = null;
            var _loc_2:* = new Vector.<TShopSell>;
            var _loc_3:* = Cache.instance.role.entityInfo.level;
            var _loc_4:* = ClockManager.instance.nowDate.time;
            var _loc_6:* = HeroArenaController.HeroArenaLevel;
            for each (_loc_7 in param1)
            {
                
                if (this.isCanSellByTshopSell(_loc_7, _loc_3, _loc_4))
                {
                    _loc_2.push(_loc_7);
                }
            }
            return _loc_2;
        }// end function

        private function getMeritArr(param1:Vector.<TShopSell>) : Vector.<TShopSell>
        {
            var _loc_5:ItemData = null;
            var _loc_6:TRune = null;
            var _loc_7:SkillInfo = null;
            var _loc_8:TShopSell = null;
            var _loc_9:TRune = null;
            var _loc_2:* = new Vector.<TShopSell>;
            var _loc_3:* = Cache.instance.role.entityInfo.level;
            var _loc_4:* = ClockManager.instance.nowDate.time;
            for each (_loc_8 in param1)
            {
                
                if (this.isCanSellByTshopSell(_loc_8, _loc_3, _loc_4))
                {
                    _loc_6 = SkillConfig.instance.getRuneByItemCode(_loc_8.itemCode);
                    if (_loc_6)
                    {
                        _loc_7 = Cache.instance.skill.getSkillBySerialId(_loc_6.skillBelong);
                        _loc_9 = Cache.instance.skill.getRuneLearnedBySkillAndPos(_loc_6.skillBelong, _loc_6.runePos);
                        if (_loc_7 && Cache.instance.skill.isRuneCanUpgrade2(_loc_6, _loc_7, _loc_9 ? (true) : (false)))
                        {
                            _loc_2.push(_loc_8);
                        }
                        continue;
                    }
                    _loc_2.push(_loc_8);
                }
            }
            return _loc_2;
        }// end function

        private function getArr(param1:Vector.<TShopSell>) : Vector.<TShopSell>
        {
            var _loc_5:ItemData = null;
            var _loc_6:TShopSell = null;
            var _loc_2:* = new Vector.<TShopSell>;
            var _loc_3:* = Cache.instance.role.entityInfo.level;
            var _loc_4:* = ClockManager.instance.nowDate.time;
            for each (_loc_6 in param1)
            {
                
                if (this.isCanSellByTshopSell(_loc_6, _loc_3, _loc_4))
                {
                    _loc_2.push(_loc_6);
                }
            }
            return _loc_2;
        }// end function

        private function isCanSellByTshopSell(param1:TShopSell, param2:int, param3:Number) : Boolean
        {
            var _loc_4:Boolean = false;
            var _loc_5:ItemData = null;
            if (param2 <= param1.maxLevelLimit && param2 >= param1.minLevelLimit || param1.maxLevelLimit == 0)
            {
                if (param3 <= param1.endDt.time && param3 >= param1.startDt.time)
                {
                    _loc_5 = new ItemData(param1.itemCode);
                    if (ItemsUtil.isFitCarrer(_loc_5))
                    {
                        _loc_4 = true;
                    }
                }
            }
            return _loc_4;
        }// end function

        private function init() : void
        {
            var _loc_1:* = ConfigManager.instance.getJSONByFileName("t_shop_sell.json");
            this.write(_loc_1);
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_shop_panic_buy_item.json");
            this.writePanic(_loc_1);
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_shop.json");
            this.writeShop(_loc_1);
            return;
        }// end function

        protected function changeToDate(param1:Object) : void
        {
            if (param1.hasOwnProperty("endDt"))
            {
                param1["endDt"] = DateParser.strToDateNormal(param1["endDt"]);
            }
            if (param1.hasOwnProperty("startDt"))
            {
                param1["startDt"] = DateParser.strToDateNormal(param1["startDt"]);
            }
            return;
        }// end function

        public function getShopSellByItemCodeAndShopCode(param1:int, param2:int = -1) : TShopSell
        {
            var _loc_3:TShopSell = null;
            var _loc_4:Dictionary = null;
            if (param2 == -1)
            {
                for each (_loc_4 in this._shopTypeItemMap)
                {
                    
                    if (_loc_4[param1])
                    {
                        _loc_3 = _loc_4[param1];
                        break;
                    }
                }
            }
            else
            {
                _loc_3 = this._shopTypeItemMap[param2][param1];
            }
            return _loc_3;
        }// end function

        public function getShopSellItemByShopCode(param1:int) : Vector.<TShopSell>
        {
            switch(param1)
            {
                case 3008:
                {
                    return this.getMeritArr(this._shopTypeItemVec[param1]);
                }
                case 3002:
                {
                    return this.getArenaArr(this._shopTypeItemVec[param1]);
                }
                case 3030:
                case 3031:
                case 3032:
                {
                    return this.get2v2ArenaArr(this._shopTypeItemVec[param1]);
                }
                default:
                {
                    return this.getArr(this._shopTypeItemVec[param1]);
                    break;
                }
            }
        }// end function

        public function getShopSellItemsByShopGroup(param1:int) : Vector.<TShopSell>
        {
            var _loc_4:Object = null;
            var _loc_5:Vector.<TShopSell> = null;
            var _loc_2:* = this.getShopTabByGroupId(param1);
            var _loc_3:* = new Vector.<TShopSell>;
            for each (_loc_4 in _loc_2)
            {
                
                _loc_5 = this.getArr(this._shopTypeItemVec[_loc_4.name]);
                _loc_3 = _loc_3.concat(_loc_5);
            }
            return _loc_3;
        }// end function

        public function getFastBuyItemByCode(param1:int, param2:int = -1) : ShopItemData
        {
            var _loc_3:ShopItemData = null;
            var _loc_4:TShopSell = null;
            var _loc_7:Array = null;
            var _loc_8:Object = null;
            var _loc_5:* = Cache.instance.role.entityInfo.level;
            var _loc_6:* = ClockManager.instance.nowDate.time;
            if (param2 > 0)
            {
                _loc_4 = this._shopTypeItemMap[param2][param1] as TShopSell;
                if (_loc_4)
                {
                    if (!this.isCanSellByTshopSell(_loc_4, _loc_5, _loc_6))
                    {
                        _loc_4 = null;
                    }
                }
            }
            else
            {
                _loc_7 = this.getShopTabByGroupId(ShopConfig.fastBuyShopGroup);
                for each (_loc_8 in _loc_7)
                {
                    
                    _loc_4 = this._shopTypeItemMap[_loc_8.name][param1] as TShopSell;
                    if (_loc_4)
                    {
                        if (this.isCanSellByTshopSell(_loc_4, _loc_5, _loc_6))
                        {
                            break;
                            continue;
                        }
                        _loc_4 = null;
                    }
                }
            }
            if (_loc_4)
            {
                _loc_3 = new ShopItemData(_loc_4);
                _loc_3.type = _loc_4.isBindMoney;
                _loc_3.isUseGoldBuyGoldBind = false;
            }
            return _loc_3;
        }// end function

        public function getRebatShopInfoByKey(param1:String) : TShopPanicBuyItem
        {
            return this._rebateItemMap[param1];
        }// end function

        public function get rebateShopMap() : Dictionary
        {
            return this._rebateShopMap;
        }// end function

        public function getTShopByCode(param1:int) : TShop
        {
            return this._shopMap[param1];
        }// end function

        public function get shopMap() : Dictionary
        {
            return this._shopMap;
        }// end function

        public function getShopTabByCode(param1:int) : Array
        {
            var _loc_4:int = 0;
            var _loc_5:Object = null;
            var _loc_6:TShop = null;
            var _loc_2:* = new Array();
            var _loc_3:* = this.getTShopByCode(param1);
            if (_loc_3)
            {
                _loc_4 = _loc_3.isMall;
                for each (_loc_6 in this._shopMap)
                {
                    
                    if (_loc_6.isMall == _loc_4)
                    {
                        _loc_5 = {name:_loc_6.code, label:_loc_6.name};
                        _loc_2.push(_loc_5);
                    }
                }
            }
            return _loc_2;
        }// end function

        public function getShopTabByGroupId(param1:int) : Array
        {
            var _loc_3:Object = null;
            var _loc_4:TShop = null;
            var _loc_2:* = new Array();
            for each (_loc_4 in this._shopMap)
            {
                
                if (_loc_4.isMall == param1)
                {
                    _loc_3 = {name:_loc_4.code, label:_loc_4.name};
                    _loc_2.push(_loc_3);
                }
            }
            return _loc_2;
        }// end function

        public static function get instance() : ShopConfig
        {
            if (_instance == null)
            {
                _instance = new ShopConfig;
            }
            return _instance;
        }// end function

    }
}
